package com.WeChat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class fragment_wechat extends Fragment {

    private RecyclerView recyclerView;
    private RecycleAdapterDome adapterDome;
    private Context context;
    private List<String> list;

    public fragment_wechat(Context context) {
        // Required empty public constructor
        this.context=context;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_wechat, container, false);


        recyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);
        list = new ArrayList<>();
        for (int i=0;i<30;i++){
            list.add("这是第"+i+"个测试");
        }
        adapterDome = new RecycleAdapterDome(context,list);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapterDome);
        return v;

    }


    public class RecycleAdapterDome extends RecyclerView.Adapter<RecycleAdapterDome.MyViewHolder> {
        private Context context;
        private List<String> list;
        private View inflater;

        public RecycleAdapterDome(Context context, List<String> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            inflater = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
            MyViewHolder myViewHolder = new MyViewHolder(inflater);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder,int position) {
            holder.textView.setText(list.get(position));
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(context,MainActivity2.class);
                    startActivity(intent);

                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView textView;
            public MyViewHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(R.id.text_view);
            }

        }
    }

}
